#include "SinglePointCrossover.hh"


int SinglePointCrossover::matesExpected() {
    return 2;
}

ArrayList<Solution> *SinglePointCrossover::cross(ArrayList<Solution> *mates) {

    if (mates->length() != 2) {
        cout << "mates must be two, in single point crossover" << endl;
        exit(1);
    }


    IntArraySolution *solution0 = (IntArraySolution*)mates->get(0);
    IntArraySolution *solution1 = (IntArraySolution*)mates->get(1);

    IntArraySolution *child0 = new IntArraySolution();
    IntArraySolution *child1 = new IntArraySolution();


    int size = IntArraySolution::getSize();
    int r = Random::getInstance()->getInt(size);

    for (int i = 0; i < size; i++) {
        if (i < r) {

            child0->set(i, solution0->get(i));
            child1->set(i, solution1->get(i));

        } else {

            child0->set(i, solution1->get(i));
            child1->set(i, solution0->get(i));

        }

    }


    ArrayList<Solution> *childs = new ArrayList<Solution>(false);
    childs->add(child0);
    childs->add(child1);

    return childs;
}
